<<<<<<< HEAD
from . import admin_bp 
from flask import request,jsonify
from app.models import UserProfile,OperationLog,ModuleType,OperationType,User
from app.extensions import db
from app.decorator import permission_required

@admin_bp.route('/admin/user_info/list',methods=['GET'])
=======
from . import admin_bp
from flask import request, jsonify
from app.models import UserProfile, OperationLog, ModuleType, OperationType, User
from app.extensions import db
from app.decorator import permission_required


@admin_bp.route('/admin/user_info/list', methods=['GET'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_USER, OperationType.QUERY)
def user_info_list():
    page = request.args.get('page', type=int)
    page_size = request.args.get('pageSize', type=int)
    search_key = request.args.get('searchKey', type=str)
    uid = request.args.get('uid', type=int)
    # 创建基础查询，加入关联查询
    query = UserProfile.query
<<<<<<< HEAD
    
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    # 如果有搜索关键词，添加过滤条件
    if search_key:
        try:
            uid_user = User.query.get(uid)
<<<<<<< HEAD
            operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_USER,operation=OperationType.QUERY,detail=f'搜索 {search_key} 的信息')
=======
            operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_USER,
                                         operation=OperationType.QUERY, detail=f'搜索 {search_key} 的信息')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
            db.session.add(operation_log)
            db.session.commit()
        except Exception as e:
            print(e)
        query = query.filter(
            UserProfile.name.like(f'%{search_key}%')
        )
<<<<<<< HEAD
    
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    # 获取分页对象
    pagination = query.paginate(
        page=page,
        per_page=page_size,
        error_out=False
    )
<<<<<<< HEAD
    
    # 获取当前页的用户列表
    users = [user.to_json() for user in pagination.items]
    
    # 获取总用户数
    total = pagination.total
    
=======

    # 获取当前页的用户列表
    users = [user.to_json() for user in pagination.items]

    # 获取总用户数
    total = pagination.total

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    data = {
        'total': total,
        'users': users
    }
<<<<<<< HEAD
    
    return jsonify({'message':'获取用户信息列表成功','errcode':'0','data':data}),200

@admin_bp.route('/admin/user_info/edit',methods=['PUT'])
=======

    return jsonify({'message': '获取用户信息列表成功', 'errcode': '0', 'data': data}), 200


@admin_bp.route('/admin/user_info/edit', methods=['PUT'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_USER, OperationType.UPDATE)
def user_info_edit():
    id = request.json.get('id')
    name = request.json.get('name')
    phone = request.json.get('phone')
    gender = request.json.get('gender')
<<<<<<< HEAD
    birthday = request.json.get('birthday') if request.json.get('birthday') else None
    address = request.json.get('address')
    uid = request.json.get('uid')
    
=======
    birthday = request.json.get(
        'birthday') if request.json.get('birthday') else None
    address = request.json.get('address')
    uid = request.json.get('uid')

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    user_info = UserProfile.query.get(id)
    user_info.name = name
    user_info.phone = phone
    user_info.gender = gender
    user_info.birthday = birthday
    user_info.address = address
    try:
        db.session.commit()
        uid_user = User.query.get(uid)
<<<<<<< HEAD
        operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_USER,operation=OperationType.UPDATE,detail=f'更新 {uid_user.email} 的信息')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message':'编辑用户信息成功','errcode':'0'}),200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message':'编辑用户信息失败','errcode':'-1','error':str(e)}),500
=======
        operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_USER,
                                     operation=OperationType.UPDATE, detail=f'更新 {uid_user.email} 的信息')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message': '编辑用户信息成功', 'errcode': '0'}), 200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message': '编辑用户信息失败', 'errcode': '-1', 'error': str(e)}), 500
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
